/*
 * EnCharge.cpp
 *
 *  Created on: 12 nov. 2012
 *      Author: guegan
 */

#include "EnCharge.h"

EnCharge* EnCharge::_instance;

void EnCharge::tourner(string direction) {
	if (!(direction == "N" || direction == "E" || direction == "O" || direction == "S"))
		throw EtatRobot::TournerException();

	robot->setDirection(direction);

	if( robot->getObstacle() != NULL )
		robot->setObstacle(NULL);
}

void EnCharge::avancer(int x, int y) {
	string dir = robot->getDirection();
	if (dir == "E") {
		if (x < robot->getPosition()->getX() || y != robot->getPosition()->getY())
			throw EtatRobot::AvancerException();
	}

	if (dir == "O") {
		if (x > robot->getPosition()->getX() || y != robot->getPosition()->getY())
			throw EtatRobot::AvancerException();
	}

	if (dir == "N") {
		if (x != robot->getPosition()->getX() || y < robot->getPosition()->getY())
			throw EtatRobot::AvancerException();
	}

	if (dir == "S") {
		if (x != robot->getPosition()->getX() || y > robot->getPosition()->getY())
			throw EtatRobot::AvancerException();
	}

	robot->setPosition(x,y);
}

int EnCharge::peser() {
	return robot->getObjet()->getPoids();
}

void EnCharge::rencontrerObstacle(Obstacle o) {
	robot->setEtatRobot(EtatRobot::getEtatRobot("EnChargeFaceObstacle"));

	robot->setObstacle(&o);
}

EnCharge* EnCharge::instance(){
    if(_instance == NULL)
          _instance = new EnCharge("EnCharge");

    return _instance;
}

EnCharge::~EnCharge() {
	_instance = NULL;
}
